package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Size;

@Service
public class SettingsService {

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private QuestionApi questionApi;

    @Reference
    private BlackListApi blackListApi;

    /**
     * 查询用户的通用设置
     */
    public ResponseEntity querySettings() {
        //1、获取当前用户
        User user = UserHolder.getUser();
        SettingsVo vo = new SettingsVo();
        //2、查询陌生人问题
        Question question = questionApi.findByUserId(user.getId());
        if(question != null) {
            vo.setStrangerQuestion(question.getTxt());
        }
        //3、查询用户的通知设置
        Settings settings = settingsApi.findByUserId(user.getId());
        if(settings != null) {
            BeanUtils.copyProperties(settings, vo);
        }
        //4、构造返回值
        vo.setPhone(user.getMobile());
        return ResponseEntity.ok(vo);
    }

    /**
     * 保存或者更新陌生问问题
     */
    public ResponseEntity saveQuestions(String content) {
        //1、根据当前登录用户查询问题
        Question question = questionApi.findByUserId(UserHolder.getUserId());
        //2、如果未设置，保存
        if(question == null) {
            question = new Question();
            question.setUserId(UserHolder.getUserId());
            question.setTxt(content);
            questionApi.save(question);
        }else {
            //3、如果以设置，更新
            question.setTxt(content);
            questionApi.update(question);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 保存或者更新 通知
     */
    public ResponseEntity updateNotification(boolean like, boolean pinglun, boolean gonggao) {
        Settings settings = settingsApi.findByUserId(UserHolder.getUserId());
        if(settings == null) {
            settings = new Settings();
            settings.setUserId(UserHolder.getUserId());
            settings.setLikeNotification(like);
            settings.setGonggaoNotification(gonggao);
            settings.setPinglunNotification(pinglun);
            settingsApi.save(settings);
        }else {
            settings.setLikeNotification(like);
            settings.setGonggaoNotification(gonggao);
            settings.setPinglunNotification(pinglun);
            settingsApi.update(settings);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询当前用户的黑名单列表
     */
    public ResponseEntity queryBlacklist(int page, int pagesize) {
        //1、获取当前用户的userid
        Long userId = UserHolder.getUserId();
        //2、调用api查询 ： Ipage
        IPage<UserInfo> iPage = blackListApi.findAll(page, pagesize, userId);
        //3、将Ipage转化为PageResult
        PageResult pr = new PageResult(page,pagesize,(int)iPage.getTotal(),iPage.getRecords());
        //4、构造返回值
        return ResponseEntity.ok(pr);
    }

    //移除黑名单
    public ResponseEntity delBlacklist(Long deleteUserId) {
        //1、获取当前用户的userid
        Long userId = UserHolder.getUserId();
        //2、调用api删除黑名单数据
        blackListApi.delete(userId,deleteUserId);
        return ResponseEntity.ok(null);
    }
}
